package com.ityueqiangu.system.controller;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ityueqiangu.core.enums.CommonEnum;
import com.ityueqiangu.core.util.StringUtils;
import com.ityueqiangu.core.web.controller.BaseController;
import com.ityueqiangu.core.web.result.DataGridResultInfo;
import com.ityueqiangu.core.web.result.ResultDataUtil;
import com.ityueqiangu.core.web.result.ResultInfo;
import com.ityueqiangu.system.domain.UserChat;
import com.ityueqiangu.system.service.IUserChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 聊天内容Controller
 *
 * @author yueqiangu
 * @date 2021-04-07
 */
@Controller
@RequestMapping("/user/chat")
public class UserChatController extends BaseController
{

    @Autowired
    private IUserChatService userChatService;


    /**
     * 查询聊天内容列表
     */
    @PostMapping("/list")
    @ResponseBody
    public DataGridResultInfo list(UserChat userChat)
    {
        startPage();
        List<UserChat> list = userChatService.selectUserChatList(userChat);
        list=list.stream().sorted(Comparator.comparing(UserChat::getCreateTime))
                .collect(Collectors.toList());
        return getDataTable(list);
    }



    /**
     * 新增保存聊天内容
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo addSave(UserChat userChat)
    {
        userChat.setType("1");
        userChat.setStatus("0");
        int i = userChatService.insertUserChat(userChat);
        UserChat query = new UserChat();
        query.setRoomId(userChat.getRoomId());
        if(i>0){
            Map<String,String> map = new HashMap<>();
            map.put("userid","T3EJAfKO");
            map.put("appid","f08c79f386995451df9dc954649fd70f");
            map.put("spoken",userChat.getContentMsg());
            String url = "https://api.ownthink.com/bot";
            String params = JSONUtil.toJsonStr(map);
            String content = HttpRequest.post(url)
                    .body(params)
                    .execute().body();
            JSONObject jsonObject = JSONUtil.parseObj(content);
            if(jsonObject.get("message").equals("success")){
                Map<String,Object> map1 = (Map<String, Object>) jsonObject.get("data");
                Map<String,Object>  map2 = (Map<String, Object>)map1.get("info");
                String str = map2.get("text").toString();
                userChat.setContentMsg(str);
                userChat.setRoomId(userChat.getRoomId());
                userChat.setId(null);
                userChat.setUserId(5);
                int j = userChatService.insertUserChat(userChat);
            }
            startPage();
            List<UserChat> list = userChatService.selectUserChatList(query);
            list=list.stream().sorted(Comparator.comparing(UserChat::getCreateTime))
                    .collect(Collectors.toList());
           return ResultDataUtil.createSuccess(CommonEnum.SELECT_SUCCESS).setData(list);
        }
      return  ResultDataUtil.createSuccess(CommonEnum.SELECT_SUCCESS).setData(null);
    }


    /**
     * 修改保存聊天内容
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResultInfo editSave(UserChat userChat)
    {
       return ResultDataUtil.createSuccess(CommonEnum.SELECT_SUCCESS).setData(userChatService.updateUserChat(userChat));
    }

    /**
     * 删除聊天内容
     */
    @PostMapping( "/remove")
    @ResponseBody
    public ResultInfo remove(String ids)
    {
        return ResultDataUtil.createSuccess(CommonEnum.SELECT_SUCCESS).setData(userChatService.deleteUserChatByIds(ids));
    }
}
